<template>
  <!-- 注册界面 -->
  <div class="container">
     <a-page-header
          title="管理员账户注册"
        />
        <p>当前管理员账户数：{{adminCount}}  &nbsp; 
          <a-tag color="green" v-if="adminCount < 2 && adminCount >= 0">
            可注册
          </a-tag>

          <a-tag color="red" v-if="adminCount >= 2">
            禁止注册
          </a-tag>
        </p>
  <a-form
    id="components-form-demo-normal-login"
    :form="form"
    class="login-form"
    @submit="handleSubmit"
  >
    <!-- <a-form-item>
      <a-input
      hidden
        v-decorator="[
          'executeAccount',
          { rules: [{ required: true, message: '请输入账户执行合约的账户地址!' }] },
        ]"
        placeholder="合约执行者账户地址"
      >
        <a-icon slot="prefix" type="environment" style="color: rgba(0,0,0,.25)" />
      </a-input>
    </a-form-item> -->
    <a-form-item>
      <a-input
        v-decorator="[
          'username',
          { rules: [{ required: true, message: '请输入注册的用户名!' }] },
        ]"
        placeholder="Username"
      >
        <a-icon slot="prefix" type="user" style="color: rgba(0,0,0,.25)" />
      </a-input>
    </a-form-item>
     
    <a-form-item>
      <a-input
        v-decorator="[
          'password',
          { rules: [{ required: true, message: '请输入注册的密码!' }] },
        ]"
        type="password"
        placeholder="Password"
      >
        <a-icon slot="prefix" type="lock" style="color: rgba(0,0,0,.25)" />
      </a-input>
    </a-form-item>
    <a-form-item>
      <a-checkbox
        v-decorator="[
          'remember',
          {
            valuePropName: 'checked',
            initialValue: true,
          },
        ]"
      >
        Remember me
      </a-checkbox>
      <a-button type="primary" html-type="submit" class="login-form-button">
        Register
      </a-button>
      Or
      <a-button type="link" @click="toLogin">
        Login in
      </a-button>
    </a-form-item>
  </a-form>
  </div>
</template>


<script>
import {getAdminUserCount, register, registerSuperUser} from "@/api/user/user"

export default {
  data(){
    return {
      adminCount: -1,
    }
  },
  beforeCreate() {
    this.form = this.$form.createForm(this, { name: 'normal_login' });
  },
  methods: {
    handleSubmit(e) {
      e.preventDefault();
      this.form.validateFields((err, values) => {
        if (!err) {
          // console.log('Received values of form: ', values);
          var param = {}
          param['username'] = values.username
          param['telephone'] = ''
          param['email'] = ''
          param['identify'] = ''
          param['role'] = 'SYS_ADMIN'
          param['executeAccount'] = values.executeAccount
          param['password'] = values.password
          console.log(param)
          registerSuperUser(param)
          .then(data =>{
            if(data.code == 500){
              this.$message.error("账号注册出错")
              return
            }
            console.log(data)
            this.$message.success("账号注册完成")
          }
          )
        }
      });
      // 简化注册流程
      
    },
    toLogin(){
        this.$router.push({name:"login"})
    },
    // 获取管理员账户数量 
    getAdminCount(){
      getAdminUserCount()
      .then(data=>{
        this.adminCount = data.data
      })
      .catch(error=>{
        this.$message.error('获取管理员数量出错');
      })
    }
  },

  created(){
    this.getAdminCount();
  }
}
</script>

<style lang="less" scoped>
.container{
    margin: auto;
    width: 400px;
    height: 700px;
    padding-top: 20px;
}

#components-form-demo-normal-login .login-form {
  max-width: 300px;
}
#components-form-demo-normal-login .login-form-forgot {
  float: right;
}
#components-form-demo-normal-login .login-form-button {
  width: 100%;
}


</style>